

from envs.models import BaseModel, Sysyems, DbInfo, InterfaceInfo
from django.db import models



class Case(BaseModel):
    RUN_CHOICES = (
        ('0', '不运行'),
        ('1', '运行'),
    )
    case_name = models.CharField(max_length=50, verbose_name="用例名称", blank=True)
    case_description = models.CharField(max_length=150, verbose_name="描述", blank=True)
    system_name = models.ForeignKey(Sysyems, verbose_name="所属系统", help_text="所属系统", on_delete=models.CASCADE)
    common_param = models.TextField(verbose_name="公共参数", blank=True)
    is_run = models.CharField(max_length=10, choices=RUN_CHOICES, verbose_name="是否运行", default=1)

    class Meta:
        db_table = 'org_case'
        verbose_name = '信息'
        verbose_name_plural = '用例信息'

    def __str__(self):
        return self.case_name


class TeSuite(BaseModel):
    ENV_TYPE_CHOICES = (('mit', 'mit'), ('sit', 'sit'), ('uat', 'uat'),)
    RUN_CHOICES = (
        ('0', '不运行'),
        ('1', '运行'),
    )
    suit_name = models.CharField(max_length=100, verbose_name="案例集名稱", blank=True)
    description = models.CharField(max_length=150, verbose_name="描述", blank=True)
    case = models.ManyToManyField(Case, verbose_name="测试案例", help_text="测试案例", blank=True)
    entype = models.CharField(choices=ENV_TYPE_CHOICES, max_length=200, verbose_name="环境类型", default='online',
                              help_text="环境类型", blank=True)
    is_run = models.CharField(max_length=10, choices=RUN_CHOICES, verbose_name="是否运行", default=1)

    class Meta:
        db_table = 'org_test_suit'
        verbose_name = '测试案例集'
        verbose_name_plural = '测试案例集'

    def __str__(self):
        return self.suit_name


class SqlScript(BaseModel):
    TYPE_CHOICES = (
        ('DQL', '查询'),
        ('DML', '修改/更新/删除'),
    )
    sql_name = models.CharField(max_length=50, verbose_name="脚本名称", blank=True)
    sql_content = models.CharField(max_length=150, verbose_name="内容", blank=True)
    db_info= models.ForeignKey(DbInfo, models.SET_NULL, null=True, verbose_name="数据库", help_text="数据库", blank=True)
    sql_type = models.CharField(choices=TYPE_CHOICES, max_length=200, verbose_name="脚本类型", default='DQL',
                              help_text="脚本类型", blank=True)
    class Meta:
        db_table = 'org_sql_script'
        verbose_name = '数据库脚本'
        verbose_name_plural = '数据库脚本'

    def __str__(self):
        return self.sql_name


class Steps(BaseModel):
    RUN_CHOICES = (
        ('0', '否'),
        ('1', '是'),
    )
    METHOD_CHOICES = (('GET', 'GET'), ('POST', 'POST'), ('PUT', 'PUT'), ('DELETE', 'DELETE'), ('PATCH', 'PATCH'))
    OPERATION_CHOICES = (('db', '数据库'), ('http', 'http请求'))
    TYPE_CHOICES = (
        ('DQL', '查询'),
        ('DML', '修改/更新/删除'),
    )
    ip = models.CharField(verbose_name="IP地址", max_length=800, help_text="IP地址", blank=True)
    port = models.CharField(verbose_name="端口号", max_length=800, help_text="端口号", blank=True)
    case_name = models.ForeignKey(Case, verbose_name="用例名称", blank=True, on_delete=models.CASCADE)
    step_description = models.CharField(max_length=150, verbose_name="步骤描述")
    operation_type = models.CharField(max_length=10, choices=OPERATION_CHOICES, verbose_name="操作类型", )
    sql_name = models.ForeignKey(SqlScript, models.SET_NULL, null=True, verbose_name="步骤脚本", blank=True)
    interface_name = models.ForeignKey(InterfaceInfo, models.SET_NULL, null=True, verbose_name="对应的接口", blank=True)
    method = models.CharField(max_length=20, choices=METHOD_CHOICES, verbose_name="请求方法", blank=True,editable=False)
    request_header = models.TextField(verbose_name="请求头", blank=True)
    request_body = models.TextField(verbose_name="请求体", blank=True)
    replace_param = models.TextField(verbose_name="替换报文", blank=True)
    # expected_result = models.TextField(verbose_name="预期结果", blank=True)
    response_header = models.TextField(verbose_name="返回头", blank=True,editable=False)
    response_body = models.TextField(verbose_name="返回体", blank=True,editable=False)
    # get_res_param = models.TextField(verbose_name="提取参数", blank=True)
    # get_res_header_param = models.TextField(verbose_name="提取响应头参数", blank=True)
    sql_content = models.CharField(max_length=150, verbose_name="内容", blank=True,editable=False)
    sql_type = models.CharField(choices=TYPE_CHOICES, max_length=200, verbose_name="脚本类型", default='DQL',
                              help_text="脚本类型", blank=True,editable=False)
    common_param = models.TextField(verbose_name="公共参数", blank=True)
    is_use_temp = models.CharField(max_length=10, choices=RUN_CHOICES, verbose_name="是否使用模板", default=1)

    is_run = models.CharField(max_length=10, choices=RUN_CHOICES, verbose_name="是否运行", default=1)

    class Meta:
        db_table = 'org_step'
        verbose_name = '用例步骤'
        verbose_name_plural = '用例步骤'

    def __str__(self):

        return self.step_description


class VerifyResult(BaseModel):
    RULE_CHOICES = (
        ('eq', '相等'),
        ('ct', '实际结果包含期望值'),
    )
    ARE_CHOICES = (
        ('header', '响应头'),
        ('body', '响应体'),
    )
    step = models.ForeignKey(Steps, related_name='verifyResults',verbose_name="用例步骤", blank=True, on_delete=models.CASCADE)
    area=models.CharField(max_length=50, choices=ARE_CHOICES,verbose_name="断言区域", blank=True,default="body")
    regex=models.CharField(max_length=50, verbose_name="正则提取公式", blank=True,editable=False)
    rule=models.CharField(max_length=10, choices=RULE_CHOICES, verbose_name="断言规则", default="eq")
    actualPath=models.CharField(max_length=50, verbose_name="JSONPath路径", blank=True)
    actualValue=models.CharField(max_length=50, verbose_name="实际结果", blank=True,editable=False)
    rule=models.CharField(max_length=10, choices=RULE_CHOICES, verbose_name="断言规则", default="eq")
    exceptValue=models.CharField(max_length=50, verbose_name="期望结果", blank=True)
    isSucess=models.CharField(max_length=50, verbose_name="是否通过", blank=True,editable=False)
    logmsg=models.CharField(max_length=50, verbose_name="日志信息", blank=True,editable=False)
    class Meta:
        db_table = 'org_step_verify'
        verbose_name = '用例断言'
        verbose_name_plural = '用例断言'

    def to_dict(self):
         return {'actualPath': self.actualPath, 'rule': self.rule, 'expect': self.exceptValue, 'actualValue': self.actualPath, 'status': self.isSucess}


class ResultParamGet(BaseModel):
    RULE_CHOICES = (
        ('eq', '相等'),
        ('ct', '实际结果包含期望值'),
    )
    ARE_CHOICES = (
        ('header', '响应头'),
        ('body', '响应体'),
    )
    step = models.ForeignKey(Steps, related_name='resultParamGets',verbose_name="用例步骤", blank=True, on_delete=models.CASCADE)
    area=models.CharField(max_length=50, choices=ARE_CHOICES,verbose_name="提取区域", blank=True,default="body")
    regex=models.CharField(max_length=50, verbose_name="正则提取公式", blank=True,editable=False)
    valuePath=models.CharField(max_length=50, verbose_name="JSONPath路径", blank=True)
    storeValue=models.CharField(max_length=50, verbose_name="存放数据名称", blank=True)

    class Meta:
        db_table = 'org_step_result_param'
        verbose_name = '结果参数提取'
        verbose_name_plural = '结果参数提取'


class CommonParamGet(BaseModel):

    suite = models.ManyToManyField(TeSuite, verbose_name="用例场景", blank=True)
    case = models.ManyToManyField(Case, verbose_name="用例", blank=True)

    type=models.CharField(max_length=50, verbose_name="变量类型", blank=True)
    key=models.CharField(max_length=50, verbose_name="变量名称", blank=True)
    value=models.CharField(max_length=50, verbose_name="变量值", blank=True)

    class Meta:
        db_table = 'org_common_param'
        verbose_name = '公共参数'
        verbose_name_plural = '公共参数'

    def __str__(self):

        return self.key
